﻿//  Symplectic Spark
//  Copyright 2010 Symplectic Ltd
//  Created by Martyn Whitwell (martyn@symplectic.co.uk)

//  This file is part of Spark.

//  Spark is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  Spark is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with Spark.  If not, see <http://www.gnu.org/licenses/>.

// $URL: https://symplectic-spark.googlecode.com/svn/trunk/website/App_Code/RequestSpec.cs $
// $LastChangedDate: 2010-05-05 14:09:38 +0100 (Wed, 05 May 2010) $
// $LastChangedRevision: 40 $
// $LastChangedBy: martyn@symplectic.co.uk $

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Specialized;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for PageSpec
/// </summary>

namespace Symplectic.Spark
{
    public class RequestSpec : NameValueCollection //Dictionary<string, string>
    {
        public HttpRequest Request { get; private set; }
        public ElementType CurrentElementType { get; private set; }
        public ElementTypeTemplate CurrentElementTypeTemplate { get; private set; }
        public Exception Exception { get; set; }
        public bool IsError
        {
            get { return (Exception != null); }
        }
        
        
        
        public RequestSpec(HttpRequest request)
            : base(10)
        {
            Exception = null;
            Request = request;

            foreach (ElementType elementType in Global.ElementTypes.Values)
            {
                if (elementType.Matcher != null) //Cannot match if there is no matcher defined (e.g. error ElementTypes)
                {
                    Match match = elementType.Matcher.Match(request.Path);

                    //root page
                    if (match.Success && elementType.Name == "root")
                    {
                        CurrentElementType = elementType;
                        CurrentElementTypeTemplate = elementType.Index;
                    }

                    //other pages
                    if (match.Success && match.Groups["name"].Success)
                    {
                        //current
                        if (!match.Groups["othercurrent"].Success)
                        {
                            CurrentElementType = elementType;
                            if (!match.Groups["iscurrent"].Success)
                                CurrentElementTypeTemplate = elementType.Index;
                        }

                        //filter
                        if (match.Groups["iscurrent"].Success || match.Groups["othercurrent"].Success)
                        {
                            this.Set(elementType.Name, HttpUtility.HtmlEncode(match.Groups["name"].Value));
                            this.Set("element-type-id", HttpUtility.HtmlEncode(match.Groups["name"].Value));
                        }
                    }
                }
            }

            if (CurrentElementType == null)
            {
                //Don't throw an Exception, set IsError, Exception and Error Template instead 
                //IsError = true;
                CurrentElementType = Global.ErrorElementType;
                CurrentElementTypeTemplate = Global.ErrorElementType.Index;
                Exception = new Exception("Could not identify Element Type from the requested URL: " + request.Path);
            }
            else
            {
                //IsError = false;
                if (CurrentElementTypeTemplate == null)
                    CurrentElementTypeTemplate = CurrentElementType.Item; //OK to assume item template if it was not set to index above.
            }

            this.Set("element-type", CurrentElementType.Name);
            this.Set("api-category", CurrentElementType.ApiCategory);
            this.Set("api-categories", CurrentElementType.ApiCategories);

            //Add the querystring keys to the requestSpec
            foreach (string key in request.QueryString.AllKeys)
            {
                //Encode to prevent Cross-Site Scripting (XSS) attacks
                this.Set(key, HttpUtility.HtmlEncode(request.QueryString[key]));
            }
        }
    }
}
